﻿Imports ACompany.OrderProcessing.Services
Imports ACompany.OrderProcessing.Model
Imports ACompany.OrderProcessing.Infrastructure
Imports StructureMap

Public Class OrderPresenter

    Private _view As IOrderDetailView
    Private _OrderService As IOrderService

    Public Sub New(ByVal view As IOrderDetailView)
        Me.New(view, ObjectFactory.GetInstance(Of IOrderService)())
    End Sub

    Public Sub New(ByVal view As IOrderDetailView, ByVal orderService As IOrderService)
        _view = view
        _OrderService = orderService
    End Sub

    Public Sub Display()

        clearInfoAndErrorMessages()

        Dim order As Order
        order = _OrderService.FindBy(_view.OrderId)

        If Not order Is Nothing Then
            UpdateViewFrom(order)
        Else
            _view.ErrorMessage(String.Format("Order Id {0} does not exist", _view.OrderId))
        End If

    End Sub

    Private Sub UpdateViewFrom(ByVal order As Order)
        _view.Order = order
        _view.CancelOrderButtonVisible = order.CanCancel
        _view.ShipOrderButtonVisible = order.CanProcess

        If order.Status = OrderStatus.placed And _
            order.HaveEnoughStockToProcess = False Then

            _view.ErrorMessage("We don't have enough stock to fulfill this order.")

        End If

    End Sub

    Public Sub cancel()
        cancel(_OrderService.FindBy(_view.OrderId))
    End Sub

    Public Sub Cancel(ByVal order As Order)

        clearInfoAndErrorMessages()

        If Not order Is Nothing Then
            If order.CanCancel Then
                order.Cancel()
                _OrderService.Save(order)
                UpdateViewFrom(order)
                _view.InfoMessage("This order has been cancelled.")
            Else
                _view.ErrorMessage("This order cannot be cancelled.")
            End If
        Else
            _view.ErrorMessage(String.Format("Order Id {0} does not exist", _view.OrderId))
        End If

    End Sub

    Public Sub Process()
        Process(_OrderService.FindBy(_view.OrderId))
    End Sub

    Public Sub Process(ByVal Order As Order)

        clearInfoAndErrorMessages()

        If Not order Is Nothing Then
            If Order.CanProcess Then

                Try
                    _OrderService.Process(Order)
                    UpdateViewFrom(Order)
                    _view.InfoMessage("This order has been processed.")
                Catch ex As ConcurrencyException
                    _view.ErrorMessage(ex.Message)
                End Try

            Else
                _view.ErrorMessage("This order cannot be processed.")
            End If
        Else
            _view.ErrorMessage(String.Format("Order Id {0} does not exist", _view.OrderId))
        End If

    End Sub

    Private Sub clearInfoAndErrorMessages()
        _view.ErrorMessage(String.Empty)
        _view.InfoMessage(String.Empty)
    End Sub

End Class
